import cocos, pyglet
from cocos.actions import *
from cocos.director import director

import models, gui, city_editor
from config import current as conf

class CityLayer(cocos.layer.Layer):
    """Draws the city, intercepts clicks on the city which open an editor layer"""

    def __init__(self, city, gamegrid):
        super(CityLayer, self).__init__()

        self.city = city
        self.gamegrid = gamegrid
        self.position = gamegrid.cell_to_pixel(city.location)

        # load the main sprite
        if city.status == models.City.CLEAN:
            label_color = conf.clean_city_color
            self.image = cocos.sprite.Sprite('city-live-small.png')
        else:
            self.image = cocos.sprite.Sprite('city-dead-small.png')
            label_color = conf.infected_city_color
        self.add(self.image)

        # add a label and a drop shadow
        self.label = cocos.text.Label(city.name,
            font_name=conf.body_font, font_size=10,
            color=label_color, x=0, y=-33,
            anchor_x='center', anchor_y='center')
        self.add(self.label, z=1)

        self.label_shadow = cocos.text.Label(city.name,
            font_name=conf.body_font, font_size=10,
            color=(0, 0, 0, 255), x=1, y=-34,
            anchor_x='center', anchor_y='center')
        self.add(self.label_shadow, z=0)

        gauge_color=(0,100,0,255)
        if city.status == models.City.INFECTED:
            gauge_color=(0,0,100,255)
        
        self.wall_gauge = gui.Gauge(
            position = (-24, -27),
            value = lambda: city.wall, adjustable=False,
            num_bars = 15, spacing = 2, font_size = 10, col = gauge_color)
        self.add(self.wall_gauge)

        self.wall_gauge.rotation = -90
        self.wall_gauge.position = (-740, 105)

        self.city.handle("update", self.update)
        self.city.handle("flip", self.flip)

        self.city.handle("attack", self.attack)
        
        self.gauge_is_wall = True

    def update(self):
        if self.city.status == models.City.CLEAN and self.gauge_is_wall and self.city.wall <= 0.0:
            self.wall_gauge.change(lambda: self.city.poprate, (100,0,0,255))
            self.gauge_is_wall = False
        elif self.city.status == models.City.CLEAN and not self.gauge_is_wall and self.city.wall > 0.0:
            self.wall_gauge.change(lambda: self.city.wall, (0,100,0,255))
        self.wall_gauge.update()
    
    def setup_graphics(self):
        pass
    
    def flip(self):
        self.remove(self.image)
        if self.city.status == models.City.CLEAN:
            label_color = conf.clean_city_color
            self.image = cocos.sprite.Sprite('city-live-small.png')
            self.wall_gauge.change(lambda: self.city.wall, (0,100,0,255))
        else:
            self.image = cocos.sprite.Sprite('city-dead-small.png')
            label_color = conf.infected_city_color
            self.wall_gauge.change(lambda: self.city.wall, (0,0,100,255))
        self.add(self.image)
        
        

    def attack(self, position):
        """Show a simple (and fairly ugly) bullet animation"""
        x,y = self.gamegrid.cell_to_pixel(position)
        x -= self.position[0]
        y -= self.position[1]
        marker = cocos.text.Label(".",
                        font_name=conf.title_font, font_size=36,
                        color=(100,0,0,255), x=0, y=18,
                        anchor_x='center', anchor_y='center')
        self.add(marker, z=4)
        marker.do(
            Accelerate(MoveTo((x,y), .2)) +
            CallFunc(lambda: self.remove(marker)))
        
    is_event_handler = True
    HIT_RADIUS = 30
    def on_mouse_press(self, sx, sy, buttons, modifiers):
        """On click, open a city editor layer for this city"""
        if self.city.status == models.City.INFECTED:
            return False
        
        if buttons != pyglet.window.mouse.LEFT:
            return False
        
        x,y = director.get_virtual_coordinates(sx,sy)
        click = cocos.euclid.Point2(x,y)
        me = cocos.euclid.Point2(*self.position)
        if me == click or me.distance(click) < self.HIT_RADIUS:
            self.parent.add(city_editor.CityEditorLayer(self.city), z=10)
            self.label.element.color = conf.clean_city_color
            return True
        return False 

    def on_mouse_motion(self, sx, sy, dx, dy):
        """Highlight the label on hover"""
        if self.city.status == models.City.INFECTED:
            return False

        x,y = director.get_virtual_coordinates(sx,sy)
        mouse = cocos.euclid.Point2(x,y)
        me = cocos.euclid.Point2(*self.position)
        if mouse == me or me.distance(mouse) < self.HIT_RADIUS:
            self.label.element.color = conf.selected_city_color
        else:
            self.label.element.color = conf.clean_city_color
        
        
        return False
